package me.youline.dataServer.service.socialAction.handler;

import java.util.List;

import me.youline.dataServer.dao.socialActionMsg.SocialActionFollowingMsgDAO;
import me.youline.dataServer.entity.socialActionMsg.SocialActionBaseMsg;
import me.youline.dataServer.entity.socialActionMsg.SocialActionFollowingMsg;
import me.youline.dataServer.service.SocialActionNotifyMsgService;
import me.youline.dataServer.service.socialAction.event.FollowingSocialActionExeEvent;
import me.youline.dataServer.service.socialAction.event.SocialActionCancelEvent;
import me.youline.dataServer.service.socialAction.event.SocialActionEvent;
import me.youline.dataServer.service.socialAction.event.SocialActionExeEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;

/**
 * 关注社交行为事件处理器，主要负责关注执行事件处理，及关注取消事件处理。
 * 该类中定义了，关注动态信息数据的构成、关注动态的通知格式、持久化关注动态、取消事件下执行的动态数据清除业务。
 * @author linhan
 *
 */
@Component
public class FollowingSocialActionEventHandler extends AbstractSocialActionEventHandler{
	 
	@Autowired
	private SocialActionNotifyMsgService socialActionNotifyMsgService;
	
	@Autowired
	private SocialActionFollowingMsgDAO followingMsgDAO;
	
	
	private static final String MSG_BODY = "@%s 关注了您，快去看看吧!";
	

	@Override
	public boolean isSupport(SocialActionEvent event) {
		if( event instanceof FollowingSocialActionExeEvent){
			return true;
		}
		if(event instanceof SocialActionCancelEvent){
			return true;
		}
		return false;
	}


	@Override
	List<Long> decideNotifiedUsers(SocialActionExeEvent event) {
		FollowingSocialActionExeEvent e = (FollowingSocialActionExeEvent) event;
		return Lists.newArrayList(e.getTargetId());
	}


	@Override
	protected void handlerCancelEvent(SocialActionCancelEvent event) {
		socialActionNotifyMsgService.deleteFollowingNotifyMsg(event.getInitiator(), event.getTargetId());
	}



	@Override
	protected void doMsgAndEventProcess(SocialActionBaseMsg msg,
			SocialActionExeEvent event) {
		Long msgId = followingMsgDAO.addSocialActionNotifyMsg((SocialActionFollowingMsg) msg);
		msg.setId(msgId);
	}

	@Override
	protected SocialActionBaseMsg extractMsgExtensionProp(
			SocialActionExeEvent event) {
		FollowingSocialActionExeEvent e = (FollowingSocialActionExeEvent) event;
		SocialActionFollowingMsg msg = new SocialActionFollowingMsg();
		msg.setNotifiedUserId(e.getTargetObject());
		return msg;
	}


	@Override
	protected String getPushFormat() {
		return MSG_BODY;
	}

}
